/*!
 * Revise for Ext JS Library 3.4.0
 * Date: 2015/06/18
 */

// Register an ajax event of 'requestcomplete' to check authorization.
Ext.Ajax.on("requestcomplete", function(conn, response, options) {
	if (response && response.getResponseHeader) {
		if (!response.getResponseHeader("sessionstatus")) {
			return true;
		}
	} else if (response && response.responseXML.URL) {
		if (response.responseXML.URL.indexOf("/index.jsp") == -1) {
			return true;
		}
	}
	// session timeout
	alert("登录已过期，请您重新登录！");
	window.location.href = Ext.baseURI + "/index.jsp";
	
	return false;
}, this);

// Remove unselectable restriction from grid panel.
if (!Ext.grid.GridView.prototype.templates) {
    Ext.grid.GridView.prototype.templates = {};
}
Ext.grid.GridView.prototype.templates.cell = new Ext.Template(
	'<td class="x-grid3-col x-grid3-cell x-grid3-td-{id} x-selectable {css}" style="{style}" tabIndex="0" {cellAttr}>',
		'<div class="x-grid3-cell-inner x-grid3-col-{id}" {attr}>{value}</div>',
	'</td>'
);

// Extend Ext plugin
Ext.ns("Ext.plugin");
Ext.plugin.download = function(config, params) {
	var defaults = {
		action: "#",
		method: "GET",
		target: "_blank"
	};
	Ext.apply(defaults, config);
	
	if (!document.getElementById("ux-download")) {
		// Create download form
		var downloadForm = document.createElement('FORM');
		downloadForm.id = "ux-download";
		downloadForm.name = "ux-download";
		downloadForm.className = 'x-hidden';
		downloadForm.action = defaults.action;
		downloadForm.method = defaults.method;
		downloadForm.target = defaults.target;
		document.body.appendChild(downloadForm);
		
		if (Ext.isObject(params)) {
			// Add sent parameters
			for (var key in params) {
				var input = document.createElement("INPUT");
				input.type = "hidden";
				input.name = key;
				input.value = params[key];
				downloadForm.appendChild(input);
			}
		}
	}
	document.getElementById("ux-download").submit();
	document.body.removeChild(document.getElementById("ux-download"));
};
